/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.bo;

import java.util.ArrayList;
import java.util.List;
import unilearn.dao.PersonDAO;
import unilearn.dao.PersonRoleDAO;
import unilearn.dao.RoleDAO;
import unilearn.vo.Person;
import unilearn.vo.PersonRole;
import unilearn.vo.Role;

/**
 *
 * @author Yat
 */
public class PersonManagement {

    private static final String DEFAULT_PASSWORD = "password";
    private PersonDAO personDAO;
    private PersonRoleDAO personRoleDAO;
    private RoleDAO roleDAO;

    public PersonManagement() {
        personDAO = null;
        personRoleDAO = null;
        roleDAO = null;
    }

    public PersonManagement(PersonDAO personDAO, PersonRoleDAO personRoleDAO, RoleDAO roleDAO) {
        this();
        this.personDAO = personDAO;
        this.personRoleDAO = personRoleDAO;
        this.roleDAO = roleDAO;
    }

    public void setPersonDAO(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }

    public void setPersonRoleDAO(PersonRoleDAO personRoleDAO) {
        this.personRoleDAO = personRoleDAO;
    }

    public void setRoleDAO(RoleDAO roleDAO) {
        this.roleDAO = roleDAO;
    }

    public List<Person> getAllPersons() throws Exception {
        return personDAO.getAll();
    }

    public Person getPerson(int id) throws Exception {
        return personDAO.get(id);
    }

    private List<Person> getAllPersonForRole(Role role) throws Exception {
        PersonRole searchRp = new PersonRole();
        searchRp.setRoleId(role.getId());
//        System.out.println("[getAllPersonForRole][0]: " + searchRp.toString());//***
        List<PersonRole> prs = personRoleDAO.find(searchRp);
        if ((prs == null) || (prs.isEmpty())) {
            return null;
        }
//        System.out.println("[getAllPersonForRole]prs: " + prs.size());//***
        List<Person> perons = new ArrayList();
        for (PersonRole pr : prs) {
            Person p = personDAO.get(pr.getPersonId());
            if (p != null) {
                perons.add(p);
            }
        }

        return perons;
    }

    public List<Person> getAllAdministrator() throws Exception {
        return this.getAllPersonForRole(this.getRole("administrator"));
    }

    public List<Person> getAllStaff() throws Exception {
        return this.getAllPersonForRole(this.getRole("staff"));
    }

    public List<Person> getAllLecturer() throws Exception {
        return this.getAllPersonForRole(this.getRole("lecturer"));
    }

    public List<Person> getAllStudent() throws Exception {
        return this.getAllPersonForRole(this.getRole("student"));
    }

    public Person createPerson(Person person, Role role) throws Exception {
        if (roleDAO.get(role.getId()) == null) {
            throw new Exception("Could not find this role.");
        }

        if ((person.getFirstName() == null) || (person.getFirstName().equals(""))) {
            throw new Exception("First Name is not emtpy.");
        }

        if ((person.getLastName() == null) || (person.getLastName().equals(""))) {
            throw new Exception("Last Name is not emtpy.");
        }

        if ((person.getEmail() == null) || (person.getEmail().equals(""))) {
            throw new Exception("E-mail is not emtpy.");
        }

        if ((person.getPassword() == null) || (person.getPassword().equals(""))) {
            person.setPassword(DEFAULT_PASSWORD);
        }

        person = personDAO.insert(person);
        if (person == null) {
            throw new Exception("Could not create person.");
        }

        personRoleDAO.insert(new PersonRole(person.getId(), role.getId()));

        return person;
    }

    public Person updatePerson(Person person) throws Exception {
        return personDAO.update(person);
    }

    public void removePerson(Person person) throws Exception {
        personDAO.delete(person);
    }

    public void setPersonRole(Person person, Role role) throws Exception {
        List<PersonRole> prs = personRoleDAO.getAll();
        PersonRole personRole = null;
        if (prs != null) {
            for (PersonRole pr : prs) {
                if (pr.getPersonId() == person.getId()) {
                    personRole = pr;
                    break;
                }
            }
        }

        if (personRole == null) {
            personRoleDAO.insert(new PersonRole(person.getId(), role.getId()));
        } else {
            personRole.setRoleId(role.getId());
            personRoleDAO.update(personRole);
        }
    }

    public void removePersonRole(Person person, Role role) throws Exception {
        List<PersonRole> prs = personRoleDAO.getAll();
        if (prs != null) {
            for (PersonRole pr : prs) {
                if (pr.getPersonId() == person.getId()) {
                    personRoleDAO.delete(pr);
                    break;
                }
            }
        }
    }

    public List<Role> getAllRole() throws Exception {
        return roleDAO.getAll();
    }

    public Role getRole(String roleName) throws Exception {
        Role role = new Role();
        role.setName(roleName);
//        System.out.println("[getRole][0]" + role.toString());//***
        List<Role> rs = roleDAO.find(role);
        if ((rs == null) || (rs.isEmpty())) {
            return null;
        }
        role = rs.get(0);
//        System.out.println("[getRole]" + role.toString());//***
        return role;
    }

    public Role getRole(int roleId) throws Exception {
        return roleDAO.get(roleId);
    }

    public Role createRole(Role role) throws Exception {
        if ((role.getName() == null) || (role.getName().equals(""))) {
            throw new Exception("Role Name is not emtpy.");
        }

        if ((role.getDescription() == null) || (role.getDescription().equals(""))) {
            throw new Exception("Role Description is not emtpy.");
        }

        return roleDAO.insert(role);
    }

    public Role updateRole(Role role) throws Exception {
        return roleDAO.update(role);
    }

    public void removeRole(Role role) throws Exception {
        roleDAO.delete(role);
    }
}
